package com.corewillsoft.fireflies.storage;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.corewillsoft.fireflies.storage.entries.MapLevelEntries;
import com.corewillsoft.fireflies.storage.entries.SavedGameLevelEntries;
import com.corewillsoft.fireflies.storage.entries.StartGameLevelEntries;
import com.corewillsoft.fireflies.util.DebugLog;

public class DbOpenHelper extends SQLiteOpenHelper {

	private static final int DB_VERSION = 4;
	private static final String DB_NAME = "game.db";

	private final String CREATE_MAP_LEVEL_TABLE = "CREATE TABLE "
			+ MapLevelEntries.TABLE_NAME + " ("
			+ MapLevelEntries._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ MapLevelEntries.LEVEL + " INTEGER NOT NULL, "
			+ MapLevelEntries.X + " INTEGER NOT NULL, "
			+ MapLevelEntries.Y + " INTEGER NOT NULL, "
			+ MapLevelEntries.WIDTH + " INTEGER NOT NULL, "
			+ MapLevelEntries.HEIGHT + " INTEGER NOT NULL, "
			+ MapLevelEntries.IMAGE_REF + " TEXT NOT NULL" + ")";

	private final String CREATE_GAME_LEVEL_TABLE = "CREATE TABLE "
			+ StartGameLevelEntries.TABLE_NAME + " ("
			+ StartGameLevelEntries._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ StartGameLevelEntries.LEVEL + " INTEGER NOT NULL, "
			+ StartGameLevelEntries.FIREFLY_IDS + " TEXT NOT NULL, "
			+ StartGameLevelEntries.FIREFLY_X + " TEXT NOT NULL, "
			+ StartGameLevelEntries.FIREFLY_Y + " TEXT NOT NULL, "
			+ StartGameLevelEntries.PORTAL_IDS + " TEXT NOT NULL, "
			+ StartGameLevelEntries.PORTAL_X + " TEXT NOT NULL, "
			+ StartGameLevelEntries.PORTAL_Y + " TEXT NOT NULL, "
			+ StartGameLevelEntries.OBSTACLE_IDS + " TEXT NOT NULL, "
			+ StartGameLevelEntries.OBSTACLE_X + " TEXT NOT NULL, "
			+ StartGameLevelEntries.OBSTACLE_Y + " TEXT NOT NULL, "
			+ StartGameLevelEntries.OBSTACLES_PARAMS + " TEXT" + ")";

	private final String CREATE_SAVE_TABLE = "CREATE TABLE "
			+ SavedGameLevelEntries.TABLE_NAME + " ("
			+ SavedGameLevelEntries._ID + " TEXT NOT NULL UNIQUE, "
			+ SavedGameLevelEntries.LEVEL + " INTEGER NOT NULL, "
			+ SavedGameLevelEntries.FIREFLY_IDS + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.FIREFLY_X + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.FIREFLY_Y + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.FIREFLY_ENERGY + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.FIREFLY_SAVED + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.FIREFLY_ALIVE + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.PORTAL_IDS + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.PORTAL_X + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.PORTAL_Y + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.OBSTACLE_IDS + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.OBSTACLE_X + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.OBSTACLE_Y + " TEXT NOT NULL, "
			+ SavedGameLevelEntries.OBSTACLES_PARAMS + " TEXT, "
			+ SavedGameLevelEntries.COMPLETED + " INTEGER NOT NULL, "
			+ SavedGameLevelEntries.SAVED_TIME + " INTEGER NOT NULL"
			+ ")";

	public DbOpenHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		DebugLog.logMethod();
		db.execSQL(CREATE_MAP_LEVEL_TABLE);
		db.execSQL(CREATE_GAME_LEVEL_TABLE);
		db.execSQL(CREATE_SAVE_TABLE);

		DebugLog.i("DB tables successfuly created!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + MapLevelEntries.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + StartGameLevelEntries.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + SavedGameLevelEntries.TABLE_NAME);

		onCreate(db);
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
	}
}
